package com.mall.pojo;

import java.time.LocalDateTime;
import java.math.BigDecimal;

public class CartItem {
    private Long id;          // 改为Long类型匹配数据库bigint
    private Long userId;      // 改为userId匹配数据库字段
    private Long skuId;       // 商品SKU ID
    private Integer count;    // 匹配数据库
    private BigDecimal price; // 添加price字段
    private String attrsText; // 添加规格文本字段
    private Boolean selected; // 使用selected而非checked
    private LocalDateTime createTime;
    private LocalDateTime updateTime;

    // 关联对象
    private goods goods;      // 修正类名为大写
    private login user;        // 修正类名为大写
    
    // 前端显示需要的字段
    private String name;      // 商品名称
    private String picture;   // 商品图片 
    
    // 结算页面需要的字段
    private BigDecimal totalPrice;    // 小计金额
    private BigDecimal totalPayPrice; // 实付小计

    // ===== Getter & Setter =====
    public Long getId() { return id; }
    public void setId(Long id) { this.id = id; }

    public Long getUserId() { return userId; }
    public void setUserId(Long userId) { this.userId = userId; }

    public Long getSkuId() { return skuId; }
    public void setSkuId(Long skuId) { this.skuId = skuId; }

    public Integer getCount() { return count; }
    public void setCount(Integer count) { this.count = count; }

    public BigDecimal getPrice() { return price; }
    public void setPrice(BigDecimal price) { this.price = price; }

    public String getAttrsText() { return attrsText; }
    public void setAttrsText(String attrsText) { this.attrsText = attrsText; }

    public Boolean getSelected() { return selected; }
    public void setSelected(Boolean selected) { this.selected = selected; }

    public LocalDateTime getCreateTime() { return createTime; }
    public void setCreateTime(LocalDateTime createTime) { this.createTime = createTime; }

    public LocalDateTime getUpdateTime() { return updateTime; }
    public void setUpdateTime(LocalDateTime updateTime) { this.updateTime = updateTime; }

    public goods getGoods() { return goods; }
    public void setGoods(goods goods) { this.goods = goods; }

    public login getUser() { return user; }
    public void setUser(login user) { this.user = user; }
    
    public String getName() { 
        return name != null ? name : (goods != null ? goods.getName() : null);
    }
    public void setName(String name) { this.name = name; }
    
    public String getPicture() { 
        return picture != null ? picture : (goods != null ? goods.getImage() : null);
    }
    public void setPicture(String picture) { this.picture = picture; }
    
    public BigDecimal getTotalPrice() { 
        // 如果已经设置了，直接返回
        if (totalPrice != null) {
            return totalPrice;
        }
        // 否则计算 price * count
        if (price != null && count != null) {
            return price.multiply(BigDecimal.valueOf(count));
        }
        return BigDecimal.ZERO;
    }
    
    public void setTotalPrice(BigDecimal totalPrice) { 
        this.totalPrice = totalPrice; 
    }
    
    public BigDecimal getTotalPayPrice() { 
        // 如果已经设置了，直接返回
        if (totalPayPrice != null) {
            return totalPayPrice;
        }
        // 否则返回小计价格
        return getTotalPrice();
    }
    
    public void setTotalPayPrice(BigDecimal totalPayPrice) { 
        this.totalPayPrice = totalPayPrice; 
    }
}